using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using GameVuTru.ManagerScreens;
using GameVuTru.ResourceManager;

using GameVuTru.Screens;
namespace GameVuTru.GameObject
{
    public class CBoom : CObject
    {
        public List<Explore> Listboomexplosion;
        float rotation;
        Vector2 starpostion;
        int countboomexplosion;//dem so vu no con lai , neu ko con vu no thi chet(islife = false)
        public bool islife;//khi boom da no thi no se chet
        public CBoom(Vector2 pos)
        {
            starpostion = pos;
            postion = pos;
            Isvisible = true;
            rotation = 0f;
            m_sprite = new CSprite(ReSouce.boom, 10, 5, 220, 72);
            typeobject = OBJECTTYPE.BOOM;
            Listboomexplosion = new List<Explore>();
            islife = true;
            countboomexplosion = 0;
        }

        public override void Update(GameTime gametime)
        {

            if (isvisible)
            {
                postion.X += ((400 - starpostion.X) / 50);
                postion.Y += ((240 - starpostion.Y) / 50);
                m_sprite.UpdateSprite(gametime, 0, 9, 1);
                //boom ngung di chuyen va no
                if (starpostion.Y >= 240 && postion.Y <= 240)
                {
                    isvisible = false;
                    Listboomexplosion.Add(new Explore(EXPLOSIONTYPE.BOOMEXPLOSION, new Vector2(400, 240)));
                    Listboomexplosion.Add(new Explore(EXPLOSIONTYPE.BOOMEXPLOSION, new Vector2(280, 240)));
                    Listboomexplosion.Add(new Explore(EXPLOSIONTYPE.BOOMEXPLOSION, new Vector2(520, 240)));
                    Listboomexplosion.Add(new Explore(EXPLOSIONTYPE.BOOMEXPLOSION, new Vector2(400, 360)));
                    Listboomexplosion.Add(new Explore(EXPLOSIONTYPE.BOOMEXPLOSION, new Vector2(400, 120)));
                }
                if (starpostion.Y <= 240 && postion.Y >= 240)
                {
                    isvisible = false;
                    Listboomexplosion.Add(new Explore(EXPLOSIONTYPE.BOOMEXPLOSION, new Vector2(400, 240)));
                    Listboomexplosion.Add(new Explore(EXPLOSIONTYPE.BOOMEXPLOSION, new Vector2(280, 240)));
                    Listboomexplosion.Add(new Explore(EXPLOSIONTYPE.BOOMEXPLOSION, new Vector2(520, 240)));
                    Listboomexplosion.Add(new Explore(EXPLOSIONTYPE.BOOMEXPLOSION, new Vector2(400, 360)));
                    Listboomexplosion.Add(new Explore(EXPLOSIONTYPE.BOOMEXPLOSION, new Vector2(400, 120)));
                }
                //update goc quay
                if (postion.X < 200 && starpostion.X < 200)
                {
                    if (starpostion.Y >= 240)
                    {
                        rotation = -1;
                    }
                    else
                    {
                        rotation = 0.75f;
                    }

                }
                if (postion.X >= 200 && postion.X < 400 && starpostion.X >= 200 && starpostion.X < 400)
                {
                    if (starpostion.Y >= 240)
                    {
                        rotation = -0.5f;
                    }
                    else
                    {
                        rotation = 1.0f;
                    }
                }
                if (postion.X >= 600 && starpostion.X >= 600)
                {
                    if (starpostion.Y >= 240)
                    {
                        rotation = 4;
                    }
                    else
                    {
                        rotation = 2.0f;
                    }
                }
                if (postion.X > 400 && postion.X < 600 && starpostion.X > 400 && starpostion.X < 600)
                {
                    if (starpostion.Y >= 240)
                    {
                        rotation = 4.5f;
                    }
                    else
                    {
                        rotation = 1.75f;
                    }
                }

            }
            countboomexplosion = 0;
            if (Listboomexplosion.Count > 0)
            {
                for (int i = 0; i < Listboomexplosion.Count; i++)
                {
                    if (Listboomexplosion[i].isvisible)
                    {
                        countboomexplosion++;
                    }
                }
                if (countboomexplosion == 0)
                {
                    islife = false;
                    GameplayScreen.boomIsexplosioned = true;
                }
            }

            //remove vu no truoc khi update
            removeListexplosion();
            //het no roi qua bom chet,se xoa khoi danh sach boom ben Player

            //update vu no boom
            foreach (Explore item in Listboomexplosion)
            {
                item.Update(gametime);
            }

        }
        public override void Draw(SpriteBatch sp)
        {
            if (isvisible)
            {
                m_sprite.DrawSprite(sp, postion, rotation);
            }
            foreach (Explore item in Listboomexplosion)
            {
                item.Draw(sp);
            }
        }
        public override void UpdateCollion(CObject b)
        {
            base.UpdateCollion(b);
        }
        public void removeListexplosion()
        {
            for (int i = 0; i < Listboomexplosion.Count; i++)
            {
                if (!Listboomexplosion[i].isvisible)
                {
                    Listboomexplosion.RemoveAt(i);
                    i--;
                }
            }
        }
    }

}
